package com.zifan.sever.controller;


import com.zifan.sever.bean.Position;
import com.zifan.sever.service.IPositionService;
import com.zifan.sever.utils.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 *  职位管理
 * </p>
 *
 * @author zifan
 * @since 2021-04-10
 */
@RestController
@RequestMapping("/system/basic/pos")
public class PositionController {

    @Autowired
    private IPositionService positionService;

    /**
     * 查询职位列表
     * @return
     */
    @GetMapping("/")
    public List<Position> getPositions(){
        return positionService.list();
    }

    /**
     * 新增一个值位
     * @param position
     * @return
     */
    @PostMapping("/")
    public RespBean addPosition(@RequestBody Position position){
        if (positionService.save(position)){
            return RespBean.success("新增成功");
        }
        return RespBean.error("更新失败");
    }

    /**
     * 根据id更新职位信息
     * @param position
     * @return
     */
    @PutMapping("/")
    public RespBean updatePosition(@RequestBody Position position){
        position.setCreateDate(LocalDateTime.now());
        if (positionService.updateById(position)){
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }

    /**
     * 根据id删除职位信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public RespBean deletePosition(@PathVariable Integer id){
        if (positionService.removeById(id)){
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/")
    public RespBean deletePositions(@RequestParam List<Integer> ids){
        if (positionService.removeByIds(ids)){
            return RespBean.success("批量删除成功");
        }
        return RespBean.error("批量删除失败");
    }



}
